package com.zdkj.daemon.quartz.util;

import com.zdkj.common.core.util.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 数据库JDBC链接方式工具类
 * @author Magic
 *
 */
public class JdbcUtil {


    /**
     * 需要执行的sql
     * @param sql 需要执行的sql
     * @param fieldstr 需要获取的字段
     * @return
     * @throws SQLException
     */
    public static List<Map<String,Object>> exeSqlQuery(String sql,String fieldstr) throws SQLException {
        String[] fields = fieldstr.split(",");
        DataSource dataSource= SpringContextHolder.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        List<Map<String,Object>> maps=new ArrayList<>();
        while(resultSet.next()){
            Map<String,Object> map=new HashMap<>();
            for(String f:fields){
                Object object = resultSet.getObject(f);
                map.put(f,object);
            }
            maps.add(map);
        }
        connection.close();
        return maps;
    }

    /**
     * 需要执行的sql
     * @param sql 需要执行的sql
     * @return
     * @throws SQLException
     */
    public static List<Map<String,Object>> exeSqlQuery(String sql) throws SQLException {
        DataSource dataSource= SpringContextHolder.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        List<String> fields=new ArrayList<>();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        for(int i=1;i<=columnCount;i++){
            String columnName = metaData.getColumnLabel(i);//别名
            if(StringUtils.isEmpty(columnName)){
                 columnName = metaData.getColumnName(i); //字段名
            }
            fields.add(columnName);
        }

        List<Map<String,Object>> maps=new ArrayList<>();
        while(resultSet.next()){
            Map<String,Object> map=new HashMap<>();
            for(String f:fields){
                Object object = resultSet.getObject(f);
                map.put(f,object);
            }
            maps.add(map);
        }
        connection.close();
        return maps;
    }


    /**
     * 需要执行的sql
     * @param sql 需要执行的sql
     * @return
     * @throws SQLException
     */
    public static Integer exeSqlCrud(String sql) throws SQLException {
        DataSource dataSource= SpringContextHolder.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(sql);
        connection.close();
        return i;
    }
}
